using UnityEngine;
using UnityEngine.UI;

public class PlayerHealthBar : MonoBehaviour
{
    public Image HP;        // 即时血量UI
    public Image slowHP;    // 缓冲血量UI

    // 注册事件监听器
    private void OnEnable()
    {
        PlayerEvents.onUpdateHP += UpdateHP;
    }

    // 注销事件监听器
    private void OnDisable()
    {
        PlayerEvents.onUpdateHP -= UpdateHP;
    }

    //更新血条UI
    public void UpdateHP(float currentHP, float maxHP)
    {
        HP.fillAmount = currentHP / maxHP;
    }

    private void Update()
    {
        // 当缓冲血量大于即时血条时，缓冲血条持续减少
        if (slowHP.fillAmount > HP.fillAmount)
        {
            // 缓冲血量持续减少，Time.daltaTime可换成其他时间参数，如0.01f
            slowHP.fillAmount -= Time.deltaTime * 0.1f;
        }
        else
        {
            // 使缓冲血量与即时血量相等
            slowHP.fillAmount = HP.fillAmount;
        }
    }
}

